<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>管理员登录</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/3.0.0/fetch.min.js"></script>  <!-- 引入Fetch API的polyfill，用于支持老版本浏览器 -->
    <style>
        body {
            font-family: '微软雅黑', Arial, sans-serif;
            background: #f7f7f7;
            margin: 0;
            padding: 20px;
        }
        .login-container {
            max-width: 300px;
            margin: 100px auto 0;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        input[type="password"] {
            padding: 10px;
            margin-top: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #5cb85c;
            color: white;
            padding: 10px;
            margin-top: 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #4cae4c;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>管理员登录</h2>
    <form id="loginForm" action="/api/admins/login" method="post">
        <div>
            <label for="adminName">管理员名称：</label>
            <input type="text" id="adminName" name="adminName" required>
        </div>
        <div>
            <label for="password">管理员密码：</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div>
            <button type="submit">登录</button>
        </div>
    </form>
</div>

<script>
    // 监听表单的submit事件
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        var formData = new FormData(this);
        fetch('/api/admins/login', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应不正确');
                }
                return response.json();
            })
            .then(data => {
                if (data.loginSuccess) { // 假设服务器返回的JSON中包含loginSuccess字段
                    window.location.href = '/adminhome'; // 登录成功，跳转到管理员主页
                } else {
                    // 显示错误消息
                    alert('凭据无效');
                }
            })
            .catch(error => {
                console.error('错误：', error);
                // 显示错误消息
                alert('登录过程中发生错误。');
            });
    });
</script>
</body>
</html>